// pages/samplesend/samplesend.ts
import api from "../../utils/api"
import animationHelper from '../..//utils/animation';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    chooseAddress: false as boolean,
    addressInfo: {} as AddressModel,
    disabled: false as boolean,//按钮是否可以点击
    sampleTypeList: [] as SampleTypeModel[],//样本类型
    showSampleType: false as boolean,//展示样本类型弹窗
    animation: null as WechatMiniprogram.Animation | null,
    animationData: {} as any,
    sampleTypeTemp: [] as SampleTypeModel,//临时选中的
    sampleTypeVal: [] as SampleTypeModel,//当前选中的的
    typePickerVal: [] as Array<number>,//类型默认选中的
    testingItemList: [] as Array<TestingItemModel>,
    testingItem: null as any,
    testingItemTemp: [] as SampleTypeModel,
    showTime: false as boolean,//展示上门时间弹窗
    timePickerVal:[] as Array<number>,// 默认选中的时间
    timeList:[] as Array<CallExpressUsableTime>,//时间列表
    timeTemp:[] as CallExpressUsableTime,//临时时间
    timeVal:[] as CallExpressUsableTime,//确定后的时间值
    canClose: true as boolean,// 是否可以关闭
    remark:"" as string,//备注
  },
  // 选择地址
  toAddress(): void {
    wx.navigateTo({
      url: '/pages/address/address?type=add'
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    wx.showModal({
      title: "温馨提示",
      content: "是否已完成样本绑定",
      cancelText: "未完成",
      confirmText: "完成",
      confirmColor: "#EF0C26",
      success: (res) => {
        if (res.confirm) {
          this.getSampleTypes();
          this.getUsableTimeList();
          setTimeout(() => {
            this.setData({
              typePickerVal: [0],
              timePickerVal:[0]
            })
          })
        }
        if (res.cancel) {
          wx.navigateBack()
        }
      }
    })
  },
  // 获取样本类型
  getSampleTypes(): void {
    api.sampleBind.getTestingItemList().then(res => {
      if (res.data.code == '000') {
        this.setData({
          testingItemList: res.data.data,
          // testingItem: res.data.data![0]
        })
      } else {
        wx.showToast({
          title: res.data.message as string,
          icon: "error",
          duration: 2000
        })
        console.error(res.data.message)
      }
    })
  },
  // 展示弹窗
  chooseType(): void {
    const animation = animationHelper.createAnimation(500, 'ease');
    this.setData({ animation: animation });
    animationHelper.showAnimation(animation, 450, this.setData.bind(this), 'showSampleType');
    if(Object.keys(this.data.testingItemTemp).length == 0) {
      this.data.testingItemTemp = this.data.testingItemList[0];
    }
  },
  // 关闭弹窗
  closeSampleDialog(): void {
    const animation = this.data.animation;
    animationHelper.closeAnimation(animation, 450, this.setData.bind(this), "showSampleType");
  },
  // 类型变化
  typeChange(e: any): void {
    const val = e.detail.value;
    this.setData({
      testingItemTemp: this.data.testingItemList[val[0]],
    })
  },
  // 确定类型
  confirmType(): void {
    if (!this.data.canClose) {
      wx.showToast({
        title: "请稍后点击",
        icon: "error"
      })
      return
    }
    const index = this.data.testingItemList.findIndex(item => item.id === this.data.testingItemTemp.id);
    this.setData({
      testingItem: this.data.testingItemTemp,
      typePickerVal: [index]
    })
    this.closeSampleDialog();
  },
  // 确认呼叫
  makeCall(): void {
    const { addressInfo, testingItem, timeVal, remark } = this.data
    if (!addressInfo.id || !testingItem.id || !timeVal.timeType) {
      wx.showToast({
        title: "存在为空的项",
        icon: 'error'
      })
      return
    }
    const params = {
      addressId: addressInfo.id,
      visitTimeType: timeVal.timeType,
      testingItemId: testingItem.id,
      remark
    }
    api.callExpress.makeCallExpress(params).then(res => {
      if (res.data.code == '000') {
        wx.navigateBack();
      } else {
        wx.showToast({
          title: res.data.message as string,
          icon: "none",
        })
        console.error(res.data.message)
      }
    })
  },
  /**
   * 可用时间列表相关
   * - getUsableTimeList：获取对应列表
   * - closeTimeDialog：关闭弹窗
   * - confirmTime：确认选择时间
   * - timeChange：时间变化
   */
  getUsableTimeList(): void {
    api.callExpress.getUsableTimeList().then(res => {
      if (res.data.code == "000") {
        this.setData({
          timeList: res.data.data
        })
      } else {
        wx.showToast({
          title: res.data.message as string,
          icon: "error"
        })
      }
    })
  },
  closeTimeDialog(): void {
    const animation = this.data.animation;
    animationHelper.closeAnimation(animation, 450, this.setData.bind(this), "showTime");
  },
  confirmTime(): void {
    if (!this.data.canClose) {
      wx.showToast({
        title: "请稍后点击",
        icon: "error"
      })
      return
    }
    const index = this.data.timeList.findIndex(item => item.timeType === this.data.timeTemp.timeType);
    this.setData({
      timeVal: this.data.timeTemp,
      timePickerVal: [index]
    })
    this.closeTimeDialog();
  },
  timeChange(e: any): void {
    const val = e.detail.value;
    this.setData({
      timeTemp: this.data.timeList[val[0]],
    })
  },
  showTime(): void {
    const animation = animationHelper.createAnimation(500, 'ease');
    this.setData({ animation: animation });
    animationHelper.showAnimation(animation, 450, this.setData.bind(this), 'showTime');
    if(Object.keys(this.data.timeTemp).length == 0) {
      this.data.timeTemp = this.data.timeList[0];
    } 
  },
  pickerStart(): void {
    this.setData({
      canClose: false
    })
  },
  pickerEnd(): void {
    this.setData({
      canClose: true
    })
  },
  // 备注输入
  handleRemark(e:any): void {
    this.setData({
      remark:e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})